package org.celstec.mobilegame.locatory;

import org.celstec.mobilegame.gamaray.Asset;
import org.celstec.mobilegame.gamaray.Overlay;
import org.celstec.mobilegame.gamaray.OverlayImg;
import org.celstec.mobilegame.server.PMF;
import org.celstec.mobilegame.server.SubmitGameServiceImpl;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.jdom.Element;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;


@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Card {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	public Key key;
	
	@Persistent
	public String cardId;
	@Persistent
	public String cardImage;
	@Persistent
	public String cardImageType;
	@Persistent
	public String cardImagePickedUp;
	@Persistent
	public String cardImagePickedUpType;

	public static Card getObject (Key k){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			return (Card) pm.getObjectById(Card.class, k);
		} finally {			
			pm.close();
		}
	}
	
	public static Card getObject (String id){
			PersistenceManager pm = PMF.get().getPersistenceManager();
			Card returnCard = null;
			try {
				String query = "select from " + Card.class.getName()+" where cardId =='"+id+"'";
				List<Card> user = (List<Card>) pm.newQuery(query).execute();
				if (user.size() != 0) {
					returnCard = user.get(0);
				} 
		} finally {			
			pm.close();
		}
		return returnCard;
	}
	
	public Card(){
	}
	
	public Card(Element card){
		this();
		if (card.getAttributeValue("cardId")!= null) cardId = card.getAttributeValue("cardId");
		if (card.getAttributeValue("cardImage")!= null) cardImage = card.getAttributeValue("cardImage");
		if (card.getAttributeValue("cardImageType")!= null) cardImageType = card.getAttributeValue("cardImageType");
		if (card.getAttributeValue("cardImagePickedUp")!= null) cardImagePickedUp = card.getAttributeValue("cardImagePickedUp");
		if (card.getAttributeValue("cardImagePickedUpType")!= null) cardImagePickedUpType = card.getAttributeValue("cardImagePickedUpType");

	}

	public String toString2(){
		return "Card: "+cardImageType+" "+cardImage+" "+cardId;
	}
	
	public String toString(){
		return cardId;
	}
	
	public Overlay getOverlay(int i, int height){
		return new OverlayImg("cardPickedUp_"+cardId, 10 + (i *70), height, "BL", "&amp;cardCollectedClick="+cardId, "oc_"+cardId);
	}
	
	public Asset getIconCardImageAsset(){
		return new Asset(cardImagePickedUpType, cardImagePickedUp, "cardPickedUp_"+cardId);
	}
	
	@Deprecated
	public CardDropOffZone getCardDropOffZone(Vector<CardDropOffZone> allZones){
		for (int i = 0; i < allZones.size(); i++) {
			if (allZones.get(i).card == this.key) return allZones.get(i);
		}
		return null;
	}
	
	public CardDropOffZone getCardDropOffZone(Set<Key> allZones){
		Iterator<Key> l = allZones.iterator();
		while (l.hasNext()) {
			CardDropOffZone cdo = (CardDropOffZone) CardDropOffZone.getObject(l.next());
			if (cdo.card.toString().equals(this.key.toString())) return cdo;
		}
		
		return null;
	}


}
